# alpine 是docker官方推荐的linux发行版，文件体积小, 但是APK源太少, 这里不使用;
# buster 是debian10代号, 这里指定buster, 方便使用自定义源
FROM docker.m.daocloud.io/library/php:8.0.30-buster

# 设置环境变量, 防止出现选择时区
ENV DEBIAN_FRONTEND=noninteractive
ENV TZ=Asia/Shanghai

# 使用国内源文件
COPY sources.list /etc/apt/sources.list

# 安装 laravel 必备扩展
RUN echo "====> 同步时间" && \
    ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone && \
    \
    echo "====> 根据 laravel 8.x 要求, 安装对应扩展" && \
    apt-get update && \
    apt-get install -y --no-install-recommends \
        libmcrypt-dev \
        libbz2-dev \
        libfreetype6-dev \
        libjpeg62-turbo-dev \
        libpng-dev \
        libgmp-dev \
        libzip-dev \
        libcurl4-openssl-dev \
        libssl-dev \
        iputils-ping && \
    docker-php-ext-configure gd --with-jpeg=/usr/include --with-freetype=/usr/include/ && \
    docker-php-ext-install -j$(nproc) gd && \
    docker-php-ext-install mysqli pdo_mysql opcache pcntl bcmath sockets gmp zip && \
    \
    echo "====> 安装 Redis 扩展, laravel 使用 predis 可不装" && \
    echo "====> 安装 Mongodb 扩展" && \
    pecl install -o -f redis && \
    pecl install -o -f mongodb && \
    docker-php-ext-enable redis mongodb && \

    echo "====> 安装常用软件" && \
    apt-get install -y --no-install-recommends \
        libmcrypt-dev \
        mlocate \
        vim \
        wget \
        curl \
        htop \
        cron \
        supervisor \
        default-mysql-client \
        nodejs \
        npm \
        file \
        git \
        iputils-ping \
        rsyslog \
        procps \
        telnet \
        zipalign \
        --reinstall ca-certificates && \
        update-ca-certificates -f && \
        pecl install mcrypt && \
        # pecl install xdebug && \
        # docker-php-ext-enable xdebug && \
    \
    echo "====> 更新 mlocate 缓存" && \
    updatedb && \
    \
    echo "====> 设置 npm 为淘宝镜像" && \
    npm config set registry http://registry.npm.taobao.org && \
    \
    echo "====> 设置用户权限" && \
    usermod -u 1000 www-data && \
    echo "软件安装完成"
#    \
#    echo "====> Clear Up" && \
#    apt-get clean && \
#    rm -rf /var/lib/apt/lists/* /tmp/* /var/tmp/*

# 复制Composer安装脚本 添加 composer
COPY composer-setup.php /home/composer-setup.php
RUN echo "====> 添加Composer" && \
    # php -r "copy('https://install.phpcomposer.com/installer', 'composer-setup.php');" && \
    # php composer-setup.php && \
    # php -r "unlink('composer-setup.php');" && \
    php /home/composer-setup.php && \
    mv composer.phar /usr/local/bin/composer && \
    /usr/local/bin/composer config -g repo.packagist composer https://mirrors.aliyun.com/composer/

# Composer 报错 proc_open(): fork failed - Cannot allocate memory
# RUN /bin/dd if=/dev/zero of=/var/swap.1 bs=1M count=1024 \
#    && /sbin/mkswap /var/swap.1 \
#    && /sbin/swapon /var/swap.1

# 安装JDK 以及 apksigner
RUN mkdir -p /usr/share/man/man1/ && \
    apt update && \
    apt search jdk && \
    apt-get install -y --no-install-recommends \
    ca-certificates-java \
    openjdk-11-jdk
#COPY jdk-8u301-linux-x64.tar.gz /root
#RUN echo "====> 安装JDK" && \
#    mkdir /usr/lib/jdk && \
#    tar xvf /root/jdk-8u301-linux-x64.tar.gz -C /usr/lib/jdk && \
#    echo "export JAVA_HOME=/usr/lib/jdk/jdk1.8.0_301" >> /root/.bashrc && \
#    echo "export JRE_HOME=\${JAVA_HOME}/jre" >> /root/.bashrc && \
#    echo "export CLASSPATH=.:\${JAVA_HOME}/lib:\${JRE_HOME}/lib" >> /root/.bashrc && \
#    echo "export PATH=\${JAVA_HOME}/bin:\$PATH" >> /root/.bashrc

# 复制运行脚本 启动cron supervisord
#COPY init.sh /usr/local/bin/init.sh

RUN echo "====> 添加计划任务" && \
    echo "* * * * * cd /data/wwwroot/app && /usr/local/bin/php artisan schedule:run >> /dev/null 2>&1" > /var/spool/cron/crontabs/www-data && \
    chmod 600 /var/spool/cron/crontabs/www-data
#    \
#    touch /usr/local/bin/init.sh && \
#    echo "#!/bin/sh" > /usr/local/bin/init.sh && \
#    echo "service rsyslog start" >> /usr/local/bin/init.sh && \
#    echo "service cron start" >> /usr/local/bin/init.sh && \
#    echo "service supervisor start" >> /usr/local/bin/init.sh && \
#    echo "====> 给启动脚本增加运行权限" && \
#    chmod +x /usr/local/bin/init.sh

#CMD ["/usr/local/bin/init.sh"]
# 启动服务, 并挂起
CMD service rsyslog start && service cron start && service supervisor start && tail -f /dev/null

WORKDIR /data/wwwroot/app
